package com.wenhai.spring.aop.features;

import com.wenhai.spring.aop.features.aspect.AspectConfiguration;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import java.util.HashMap;
import java.util.Map;

/**
 * AspectJ 演示
 *
 * @author wenhai
 * @date   2021/3/4
 */
@Aspect
@Configuration
@EnableAspectJAutoProxy
public class AspectJAnnotationUsingAPIDemo {
    public static void main(String[] args) {
        Map<String, String> cache = new HashMap<>();
        AspectJProxyFactory proxyFactory = new AspectJProxyFactory(cache);
        proxyFactory.addAspect(AspectConfiguration.class);
        proxyFactory.addAdvice((MethodBeforeAdvice) (method, args1, target) -> {
            if ("put".equals(method.getName()) && args1.length == 2) {
                System.out.printf("当前存放是 Key: %s,Value: %s \n", args1[0], args1[1]);
            }
        });
        Map<String, String> cacheProxy = proxyFactory.getProxy();
        cacheProxy.put("1", "1");
        cacheProxy.get("1");
    }

}
